package com.linxianqin.springbootVue.controller.system;


import com.linxianqin.springbootVue.domain.constant.Constants;
import com.linxianqin.springbootVue.domain.model.AjaxResult;
import com.linxianqin.springbootVue.domain.dto.SysMenuDTO;
import com.linxianqin.springbootVue.domain.dto.SysUserDTO;
import com.linxianqin.springbootVue.domain.model.LoginBody;
import com.linxianqin.springbootVue.domain.model.LoginUser;
import com.linxianqin.springbootVue.domain.vo.RouterVo;
import com.linxianqin.springbootVue.service.SysMenuService;
import com.linxianqin.springbootVue.service.webService.SysLoginService;
import com.linxianqin.springbootVue.service.webService.SysPermissionService;
import com.linxianqin.springbootVue.service.webService.TokenService;
import com.linxianqin.springbootVue.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;


 /**
 * @author lxq
 * @version 1.0
 * @description: 登录验证
 * @date 2020/12/15 11:30
 */
@Slf4j
@RestController
public class SysLoginController
{
    private final SysLoginService loginService;
    private final SysMenuService menuService;
    private final SysPermissionService permissionService;
    private final TokenService tokenService;
    public SysLoginController(SysLoginService loginService,SysMenuService menuService,SysPermissionService permissionService,TokenService tokenService){
        this.loginService=loginService;
        this.menuService=menuService;
        this.permissionService=permissionService;
        this.tokenService=tokenService;
    }
    /**
     * 登录方法
     * @param loginBody 登录信息
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        log.debug("loginBody:{}",loginBody);
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody);
        log.info("token:{}",token);
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        SysUserDTO user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户菜单信息
        List<SysMenuDTO> menus = menuService.selectMenuTreeByUserId(loginUser.getUser().getUserId());
        // 路由信息
        return AjaxResult.success(menuService.buildMenus(menus));
    }

}
